package org.example.rpcendpoint;

import org.apache.pekko.actor.AbstractActor;
import org.apache.pekko.actor.ActorSelection;
import org.example.ContantVariable;
import org.example.Message;
import org.example.inter.RpcEndpoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * @className: PekkoRpcEndpoint
 * @Description:
 * @Author: wangyifei
 * @Date: 2025/3/14 9:59
 */
public class PekkoRpcEndpoint extends AbstractActor implements RpcEndpoint {
    private static Logger logger = LoggerFactory.getLogger(PekkoRpcEndpoint.class);

    @Override
    public void tell(Message msg) {
        ActorSelection actorSelection
                = context().actorSelection("pekko://"+ ContantVariable.PEKKO_SYSTEM_NAME+"@127.0.0.1:17355/user/" + ContantVariable.FACTORY_URL);
        actorSelection.tell(msg , getSelf());
    }

    @Override
    public void ask(Message msg) {

    }

    @Override
    public Receive createReceive() {
        return receiveBuilder()
                .match(Message.class , this::messageHandler)
                .build();
    }

    private void messageHandler(Message message) {
        switch (message.getType()) {
            case INVOCATION:
                tell(message);
                break;
            default:
                break;
        }
    }
}
